﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace ajayumi.develop.win32.Transport.Utils.WinForm
{
    /// <summary>
    /// 控件辅助类
    /// Author  : 黄乙冬
    /// Date    : 2011-08-30
    /// </summary>
    public class ControlHelper
    {
        #region Value property

        public static void SetValue(ProgressBar pgb, int value)
        {
            if (pgb.InvokeRequired)
            { pgb.Invoke(new Action<ProgressBar, int>(SetValue), new object[] { pgb, value }); }
            else
            { pgb.Value = value; }
        }

        public static void SetValue(NumericUpDown nud, int value)
        {
            if (nud.InvokeRequired)
            { nud.Invoke(new Action<NumericUpDown, int>(SetValue), new object[] { nud, value }); }
            else
            { nud.Value = value; }
        }

        #endregion

        #region Visible property

        public static void SetVisible(ProgressBar pgb, bool value)
        {
            if (pgb.InvokeRequired)
            { pgb.Invoke(new Action<ProgressBar, bool>(SetVisible), new object[] { pgb, value }); }
            else
            { pgb.Visible = value; }
        }

        public static void SetVisible(Button btn, bool value)
        {
            if (btn.InvokeRequired)
            { btn.Invoke(new Action<Button, bool>(SetVisible), new object[] { btn, value }); }
            else
            { btn.Visible = value; }
        }

        #endregion

        #region Enable property

        public static void SetEnable(Button btn, bool value)
        {
            if (btn.InvokeRequired)
            { btn.Invoke(new Action<Button, bool>(SetEnable), new object[] { btn, value }); }
            else
            { btn.Enabled = value; }
        }

        public static void SetEnable(TreeView tv, bool value)
        {
            if (tv.InvokeRequired)
            { tv.Invoke(new Action<TreeView, bool>(SetEnable), new object[] { tv, value }); }
            else
            { tv.Enabled = value; }
        }

        #endregion

        #region Text property

        public static void SetText(TextBox txt, string text)
        {
            if (txt.InvokeRequired)
            { txt.Invoke(new Action<TextBox, string>(SetText), new object[] { txt, text }); }
            else
            { txt.Text = text; }
        }

        public static void SetText(Label lb, string text)
        {
            if (lb.InvokeRequired)
            { lb.Invoke(new Action<Label, string>(SetText), new object[] { lb, text }); }
            else
            { lb.Text = text; }
        }

        #endregion

        #region Maximun property

        public static void SetMaximun(NumericUpDown nud, int value)
        {
            if (nud.InvokeRequired)
            { nud.Invoke(new Action<NumericUpDown, int>(SetMaximun), new object[] { nud, value }); }
            else
            { nud.Maximum = value; }
        }

        public static void SetMaximun(ProgressBar pgb, int value)
        {
            if (pgb.InvokeRequired)
            { pgb.Invoke(new Action<ProgressBar, int>(SetMaximun), new object[] { pgb, value }); }
            else
            { pgb.Maximum = value; }
        }

        #endregion

        /// <summary>
        /// 设置控件的双缓冲方式
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="value">布尔值，true表示开启，false表示关闭</param>
        public static void DoubleBuffered(Control control, bool value)
        {
            Type dgvType = control.GetType();
            PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
            pi.SetValue(control, value, null);
        }
    }
}
